Completed
Push — master ( e65836...fa4d5c )
by Mathieu
12s queued 10s
created

GetCustomerByIdQueryHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 18 2
1
import {QueryHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {GetCustomerByIdQuery} from './GetCustomerByIdQuery';
4
import {ICustomerRepository} from 'src/Domain/Customer/Repository/ICustomerRepository';
5
import {CustomerView} from '../View/CustomerView';
6
import {CustomerNotFoundException} from 'src/Domain/Customer/Exception/CustomerNotFoundException';
7
import {AddressView} from '../View/AddressView';
8
9
@QueryHandler(GetCustomerByIdQuery)
10
export class GetCustomerByIdQueryHandler {
11
  constructor(
12
    @Inject('ICustomerRepository')
13
    private readonly customerRepository: ICustomerRepository
14
  ) {}
15
16
  public async execute(query: GetCustomerByIdQuery): Promise<CustomerView> {
17
    const customer = await this.customerRepository.findOneById(query.id);
18
    if (!customer) {
19
      throw new CustomerNotFoundException();
20
    }
21
22
    const address = customer.getAddress();
23
24
    return new CustomerView(
25
      customer.getId(),
26
      customer.getName(),
27
      new AddressView(
28
        address.getId(),
29
        address.getStreet(),
30
        address.getCity(),
31
        address.getZipCode(),
32
        address.getCountry()
33
      )
34
    );
35
  }
36
}
37